const Koa = require("koa");
const server = new Koa();
const router = require("koa-router")();
const parser = require("koa-parser");
const cors = require("koa2-cors");
server.use(cors())
server.use(parser());
const views = require("koa-views");
const nunjucks = require("nunjucks");
server.use(views(__dirname + "/views" , {
    map : {html : "nunjucks"}
}))
const static = require("koa-static");
server.use(static(__dirname + "/public"))

let studentList = [
                    {
                        name:"小明",
                        password:"111",
                        e_mail:"171161325@qq.com",
                        sex:["男"],
                        hobby:["篮球"]
                    }
                ]

router.get("/" , async ctx => {
    // await ctx.render("index")
    ctx.body = studentList
})

router.get("/student" , async ctx => {
    ctx.body = studentList
})  
router.post("/student" , async ctx => {
    let student = ctx.request.body.student;
    studentList.push(student);
    ctx.body = studentList
    ctx.body = true
    // console.log(studentList)
    
})
router.delete("/student/:name" , async ctx => {
    let name = ctx.params.name;
    // studentList.splice(id,1);
    // let fruit = ctx.reequest.fruit;
    studentList.map((item,index)=>{
        if(item.name == name){
            studentList.splice(index,1);
        }
    })
    ctx.body = true
})




server.use(router.routes());
server.listen(3000 , () => {
    console.log("端口：3000")
})